<?php
/*
	家谱中心
*/
class ClanController extends BaseController
{
	public function indexAction()
	{
		$this->checkAuth();//数据检测
		$no = isset($_GET['no'])? $_GET['no']:null;
		$title = '家族中心';
		$Get = '';
		if($no){
			$center = new Center();
			$cData = Center::findFirst('gen_no="'.$no.'"');
			$title = $cData->gen_title;
			$Get = '?genno='.$no;
		}
		
		parent::load([
			'title' => $title,
			'css'   => 'index',
			'js'    => 'index'
		]);
		
		//$this->debug('select * from net_user where user_code="'.$this->infoUser('user_code').'"');
		
		$db = $this->getDbObj();
		$data = $db->fetchOne('select * from net_user where user_code="'.$this->infoUser('user_code').'"');
		//$this->debug($data);
		if(empty($data['gen_no'])) $nav = '<a href="javascript:void(0);" class="click_iframe" url="/TeClan/Clan/crtgen'.$Get.'" title="新增家谱">创建家谱</a>';
		//else
		$nav .= '
			<a href="/TeClan/Zibei/index'.$Get.'">字辈</a>
			<a href="/TeClan/Node/index'.$Get.'">族人</a>
		';
		$this->view->setVars(array(
			'nav'   => $nav,
			'desc' => $this->ClanSummary($cData)
		));		
	}
	//	index/概述
	public function ClanSummary($clan)
	{
		if(!is_object($clan)) return '';
		$clan = $clan->toArray();
		$xhtml = '';
		if(!isset($clan['gen_no'])) return $xhtml;
		$no = $clan['gen_no'];
		$xhtml = '
			<h4>'.$clan['gen_title'].'</h4>
			<p class="the_clan_edit"><a href="JavaScript:void(0);" class="click_iframe" url="/TeClan/Clan/crtgen?genno='.$clan['gen_no'].'&mode=E" title="修改|'.$clan['gen_title'].'">修改</a></p>
			<p>创建者:'.$clan['user_name'].'，创建时间：'.$clan['mtime'].'，最新维护时间：'.$clan['lately'].'</p>
			<div class="gen_desc">'.$clan['gen_desc'].'</div>
			<div class="editlog">'.$clan['editlog'].'</div>
		';
		$muser = $this->infoUser('user_name');
		//更新统计值-家族人数
		if(empty($clan['sum_person'])){
			$editlog = $clan['editlog'];			
			$center = Center::findFirst('gen_no="'.$no.'"');
			$persum = $this->db->fetchColumn('select count(*) from gen_node where gen_no="'.$no.'"');
			$center->sum_person = $persum;
			$center->lately = date('Y-m-d H:i:s');
			$editlog .= '<p>'.$muser.'于'.date('Y-m-d H:i:s').'生成了家谱人数统计值。生成值为'.$persum.'。</p>';
			$center->editlog = $editlog;
			$center->update();
			$clan['sum_person'] = $persum;
		}else $persum = $clan['sum_person'];
		//更新统计值-字辈代系总数
		if(empty($clan['sum_class'])){
			$center = Center::findFirst('gen_no="'.$no.'"');
			$editlog = $center->editlog;
			$zbsum = $this->db->fetchColumn('select count(*) from gen_zibei where gen_no="'.$no.'"');
			$editlog .= '<p>'.$muser.'于'.date('Y-m-d H:i:s').'生成了家谱字辈系数统计值。生成值为：'.$zbsum.'。</p>';
			$center->editlog = $editlog;
			$center->lately = date('Y-m-d H:i:s');
			$center->sum_class = $zbsum;
			$center->update();
		}else $zbsum = $clan['sum_class'];
		//debug($clan);
		$xhtml .= '<p>家谱加入数人数：'.$persum.',家谱字辈包含人数：'.$zbsum.'。<a href="JavaScript:void(0);" id="update_sum_data">更新统计</a><span style="display:none;" id="get_page_genno">'.$no.'</span></p>';
		//	字辈清单
		$zblist = '';
		$zbs = Zibei::find(array(
			'gen_no="'.$no.'"',
			'order'	=> 'ser_no desc'
		));
		foreach($zbs as $v){
			$zblist .= '<li><a href="javascript:void(0);" class="zibei" url="/TeClan/Clan/zb2node?no='.$v->zibei_no.'">'.$v->zibei.'('.$v->pinyin.')</a><span class="mtime">'.$v->mtime.'</span></li>';
		}
		if($zblist) $xhtml .= '<ul class="zibei_list" id="zibei_list">'.$zblist.'</ul>';
		return $xhtml;
	}
	/*提交表单-创建家族*/
	public function crtgenAction()
	{
		$this->checkAuth();//数据检测
		parent::load([
			'title'=>'创建家谱',
			'js'=>'crtgen'
		]);
		if($this->request->isPost()){
			$genno = $this->request->getPost('gen_no');
			$gen_title = $this->request->getPost('gen_title');
			$gen_desc = $this->request->getPost('gen_desc');
			$uInfo = $this->infoUser();
			if($genno){//修改
				$cter = Center::findFirst('gen_no="'.$genno.'"');
				$editlog = $cter->editlog;
				if($gen_title == $cter->gen_title && $gen_desc == $cter->gen_desc){
					$this->flash->error('系统检测到您对家谱未做任何改动，此次修改无效！');
					return false;
				}
				$cter->gen_title = $gen_title;
				$cter->gen_desc = $gen_desc;
				$cter->lately = date('Y-m-d H:i:s');
				$editlog .= $uInfo['user_name'].'于修改了家谱信息，IP来源记录'.getIp().'，时间'.date('Y-m-d H:i:s').'。<br>';
				$cter->editlog = $editlog;
				if($cter->update() == false){
					foreach ($cter->getMessages() as $message) {
						echo $message->getMessage(), "<br/>";
					}
				}
				else $this->flash->error('家谱修改成功！');
				return false;
			}			

			
			$center = new Center();
			$center->gen_title = $gen_title;
			$center->gen_desc = $gen_desc;
			$center->user_code = $uInfo['user_code'];
			$center->user_name = $uInfo['user_name'];
		
			if ($center->save() == false) {
				$this->flash->error('数据提交失败！');
				foreach ($center->getMessages() as $message) {
					echo $message->getMessage(), "<br/>";
				}
                return false;
			}else{
				$this->flash->error('家谱创建成果！');
			}
			
		}
	}
	/**	字辈影视家人	**/
	public function zb2nodeAction()
	{
		if(!isset($_GET['no'])) $this->response->redirect('error/show403'); //url 跳转
		$this->checkAuth();//数据检测
		parent::load([
			'title'=>'创建家谱',
			'js'=>'zb2node',
			'css'=>'zb2node',
		]);
		$zibei = $_GET['no'];
		$nds = Node::find('zibei_no="'.$zibei.'"');
		$xhtml = '';
		foreach($nds as $v){
			$xhtml .= '<li><a href="javascript:void(0);" class="zbno" no="'.$v->pers_id.'">'.$v->name.'</a></li>';
		}
		if($xhtml) $xhtml = '<ul>'.$xhtml.'</ul>';
		$this->view->setVars(array(
			'nodes' => $xhtml
		));
	}
	public function ajaxAction()
	{
		$data = ajax();
		$i = isset($data['i1'])? $data['i1']:null;
		if($i == 'update_sum_data'){
			$no = $data['no'];
			$cter = Center::findFirst('gen_no="'.$no.'"');
			$persum = $this->db->fetchColumn('select count(*) from gen_node where gen_no="'.$no.'"');
			$cter->sum_person = $persum;
			$cter->lately = date('Y-m-d H:i:s');
			$zbsum = $this->db->fetchColumn('select count(*) from gen_zibei where gen_no="'.$no.'"');
			$cter->sum_class = $zbsum;
			if($cter->update() == false) $ret = 'N';
			else $ret = 'Y';
			die($ret);
		}elseif($i == 'zb2node/Zbno'){
			$id = $data['perid'];
			$ret .= $this->family($id);
			//$ret = $id;
			die($ret);
		}
	}
	//	非递归加载家庭树图
	public function family($id)
	{
		$xhtml = '';//	
		$curxhml = '';//	当前同一列名称
		$chxhtml = '';//	子列显示
		$pxhtml = '';//	父列显示
		$nd = Node::findFirst('pers_id="'.$id.'"');
		$displayMamas = false;//	是否已经显示母亲
		if($nd->father != 0){
			$fnd = Node::findFirst('pers_id="'.$nd->father.'"');
			if($fnd->father != 0){
				$gfnd = Node::findFirst('pers_id="'.$fnd->father.'"');
				$pxhtml .= '<li>'.$gfnd->name.'</li>';
				$bnds = Node::find(array(
					'father="'.$gfnd->pers_id.'"',
					'order' => 'ser_no asc'
				));	
				foreach($bnds as $n){
					if($nd->father == $n->pers_id){
						$xhtml .= '<li class="cur_parent">'.$n->name.'</li>';
						if($nd->mother != 0){
							$mnd = Node::findFirst('pers_id="'.$nd->mother.'"');
							$xhtml .= '<li class="cur_parent">'.$mnd->name.'</li>';
							$displayMamas = true;
						}
					}
					else $xhtml .= '<li><a href="javascript:void(0);" class="nd2curre" no="'.$n->pers_id.'">'.$n->name.'</a></li>';
				}	
			}else $xhtml .= '<li>'.$fnd->name.'</li>';
			$chldNd = Node::find(array(
				'father="'.$nd->father.'"',
				'order' => 'ser_no asc'
			));			
			foreach($chldNd as $n){
				if($id == $n->pers_id){
					$curxhml .= '<li class="cur_persion">'.$n->name.'</li>';
					//	列举所有子列
					$isMother = false;
					$cnds = Node::find(array(
						'father="'.$id.'"',
						'order' => 'ser_no asc'
					));	
					if(count($cnds)<1){
						$cnds = Node::find(array(
							'mother="'.$id.'"',
							'order' => 'ser_no asc'
						));	
						$isMother = true;
					}
					$arr = array();
					foreach($cnds as $v){
						$chxhtml .= '<li><a href="javascript:void(0);" class="nd2curre" no="'.$v->pers_id.'">'.$v->name.'</a></li>';
						if($v->mother != 0 && !in_array($v->mother,$arr) && !$isMother){
							$cndsMother = Node::findFirst('pers_id="'.$v->mother.'"');
							if(count($cndsMother) == 1){
								$curxhml .= '<li class="cur_persion">'.$cndsMother->name.'</li>';
								$arr[] = $v->mother;
							}
						}elseif($v->father != 0 && !in_array($v->father,$arr) && $isMother){
							$cndsFather = Node::findFirst('pers_id="'.$v->father.'"');
							if(count($cndsFather) == 1){
								$curxhml .= '<li class="cur_persion">'.$cndsFather->name.'</li>';
								$arr[] = $v->father;
							}
						}

					}
					if($chxhtml) $chxhtml = '<p class="line_break"></p><ul class="cur_children">'.$chxhtml.'</ul>';				
				}
				else $curxhml .= '<li><a href="javascript:void(0);" class="nd2curre" no="'.$n->pers_id.'">'.$n->name.'</a></li>';
			}
			if($curxhml) $curxhml = '<p class="line_break"></p><ul class="children">'.$curxhml.'</ul>';
		}
		if($nd->mother != 0 && !$displayMamas){
			$mnd = Node::findFirst('pers_id="'.$nd->mother.'"');
			$xhtml .= '<li>'.$mnd->name.'</li>';
		}
		if($xhtml) $xhtml = '<ul class="parent">'.$xhtml.'</ul>';
		$xhtml .= $curxhml;
		$xhtml .= $chxhtml;
		return $xhtml;
	}
	public function testAction()
	{
		//echo '<pre>';
		//print_r($this->di);
		//print_r($this->db);//配置文件
		//$cars = $this->modelsManager->executeQuery("SELECT * FROM net_user where user_code=\"346185011\"");
		//print_r($cars);
		
		$db = $this->getDbObj();
		$data = $db->fetchAll('select * from net_user where user_code="346185011"');
		//$data->setFetchMode(Phalcon\Db::FETCH_ASSOC);
		//$data->setFetchMode(Phalcon\Db::FETCH_ASSOC);
		echo '<pre>';
		print_r($data);
		//$this->query('select * from net_user where user_code="346185011"');
		print_r($db->fetchOne('select * from net_user where user_code="346185011"'));
		print_r($db->fetchAll('select * from net_user where user_code="346185011"'));
	}
}
